﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;

namespace CCPSIP.AddressBook
{
    /// <summary>CCPSIP_064：取得員工照片 API</summary>
    /// idHeader = 裝置 UUID 前八碼。iOS 請使用 [CCPCrypto getIdHeader] 呼叫。
    /// checkcode = 裝置限時驗證碼。iOS 請使用 [CCPCrypto getCheckcode] 呼叫。
    /// PPS_Code = 欲取得員工照片的員工編號。

    public class get_personal_image : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】取得POST參數，驗證身分
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string PPS_Code = req.Form["PPS_Code"];

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                    throw new Exception("Error_01: Cannot identify this device.");  //無法識別此裝置

                //檢查參數
                if (string.IsNullOrEmpty(PPS_Code))
                    throw new Exception("Error_02: Parameter error.");  //參數錯誤

                //檢查PPS_Code是否存在
                string PPS_Code_Exist = OracleKitUser.getEmpName(PPS_Code);
                if (string.IsNullOrEmpty(PPS_Code_Exist))
                    throw new Exception("Error_03: PPS Code does not exist.");

                //【02】從"D:\CCP\PPS_IMAGE\Current"目錄下取得人員到職照片
                string TargetPath = ChrisKit.RootDir + "CCP\\PPS_IMAGE\\Current\\";
                TargetPath += PPS_Code + ".jpg";

                //檢查人員照片是否存在
                FileInfo fi = new FileInfo(TargetPath);
                if (!fi.Exists)
                    throw new Exception("Error_02: Image does not exist.");

                //【99】回傳檔案內容
                //回傳檔案
                context.Response.ContentType = "application/octet-stream";
                context.Response.Charset = null;
                context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + PPS_Code + ".jpg");
                context.Response.TransmitFile(TargetPath);
                context.Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);

                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}